package map.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MyLocationConfiguration
import com.baidu.mapapi.search.core.PoiInfo
import com.baidu.mapapi.search.poi.PoiDetailResult
import com.baidu.mapapi.search.poi.PoiDetailSearchResult
import com.google.gson.reflect.TypeToken
import com.zjw.zy.ext.launchInIo
import com.zjw.zy.smi.JustWithIt
import com.zjw.zy.utils.GsonUtils
import com.zjw.zy.utils.SharePrefUtils
import map.bean.SuggestInfoWrap
import map.default.DefaultGetPoiSearchResultListener
import map.ext.locationManager
import map.ext.mapManager
import map.ext.searchManager
import kotlin.collections.ArrayList

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/10/25 11:46
 * @description：
 */
class SuggestSearchViewModel : MapBaseViewModel() {

    companion object {
        const val SUGGEST_HISTORY_KEY = "SUGGEST_HISTORY_KEY"
    }

    //选中的搜索poi
    private val mPoiDetail: MutableLiveData<PoiDetailResult?> = MutableLiveData()
    fun getPoiDetails(): LiveData<PoiDetailResult?> = mPoiDetail

    private val mMapTouchListener = BaiduMap.OnMapTouchListener {
        locationManager().locationMode(MyLocationConfiguration.LocationMode.NORMAL)

        //传入null，隐藏详情页
        mPoiDetail.postValue(null)
    }


    private val mMapPoiClickListener = JustWithIt<PoiInfo> {
        //深度搜索
        launchInIo {
            searchManager().poiDetailSearch(it.uid, object : DefaultGetPoiSearchResultListener {
                override fun onGetPoiDetailResult(p0: PoiDetailSearchResult?) {
//                    p0?.let {
//                        mPoiDetail.postValue(p0)
//                    }
                }

                override fun onGetPoiDetailResult(p0: PoiDetailResult?) {
                    p0?.let {
                        mPoiDetail.postValue(p0)
                    }
                }
            })
        }
    }


    override fun onPageEnter() {
        super.onPageEnter()
        mapManager().addPoiOverLayClickListener(mMapPoiClickListener)
        mapManager().addMapOnTouchListener(mMapTouchListener)
    }

    override fun onPageExit() {
        super.onPageExit()
        mapManager().removePoiOverLayClickListener(mMapPoiClickListener)
        mapManager().removeMapOnTouchListener(mMapTouchListener)
    }
}